unit materialObra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DBCtrls, Grids, DBGrids, RXDBCtrl, Mask,
  ToolEdit, Provider, SqlExpr, DB, DBClient, DBLocal, DBLocalS, ppCtrls,
  ppBands, ppVar, ppPrnabl, ppClass, ppCache, ppProd, ppReport, ppDB,
  ppComm, ppRelatv, ppDBPipe, DateUtils, ppTypes, ExtCtrls, ppParameter;

type
  TfrmMaterialObra = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    Label1: TLabel;
    identificador: TEdit;
    grdMaterial: TRxDBGrid;
    Label3: TLabel;
    total: TDBText;
    btnVistaPrevia: TBitBtn;
    chbTodos: TCheckBox;
    cds: TSQLClientDataSet;
    s: TDataSource;
    ppSueldosObra: TppDBPipeline;
    rpSueldosObra: TppReport;
    ppHeaderBand1: TppHeaderBand;
    ppDBImage1: TppDBImage;
    ppLabel8: TppLabel;
    ppEmail: TppLabel;
    ppTe: TppLabel;
    ppDBText3: TppDBText;
    ppDBText6: TppDBText;
    ppLine4: TppLine;
    ppLabel5: TppLabel;
    ppLabel6: TppLabel;
    ppLabel7: TppLabel;
    ppLine1: TppLine;
    ppSystemVariable1: TppSystemVariable;
    ppLabel2: TppLabel;
    ppLabel3: TppLabel;
    ppDetailBand1: TppDetailBand;
    ppLine3: TppLine;
    datMaterial: TppDBText;
    datUnitario: TppDBText;
    datSubtotal: TppDBText;
    ppDBText1: TppDBText;
    ppDBText2: TppDBText;
    ppFooterBand1: TppFooterBand;
    ppLabel1: TppLabel;
    ppDBCalc1: TppDBCalc;
    ppLabel4: TppLabel;
    ppDBCalc2: TppDBCalc;
    ppLabel9: TppLabel;
    ppDBCalc3: TppDBCalc;
    ppEmpresa: TppDBPipeline;
    ppEmpresappField1: TppField;
    ppEmpresappField2: TppField;
    ppEmpresappField3: TppField;
    ppEmpresappField4: TppField;
    ppEmpresappField5: TppField;
    ppEmpresappField6: TppField;
    ppEmpresappField7: TppField;
    ppEmpresappField8: TppField;
    ppEmpresappField9: TppField;
    ppEmpresappField10: TppField;
    ppEmpresappField11: TppField;
    ppEmpresappField12: TppField;
    ppEmpresappField13: TppField;
    ppEmpresappField14: TppField;
    ppEmpresappField15: TppField;
    sEmpresa: TDataSource;
    cdsMATERIAL: TStringField;
    cdsCANTIDAD: TFloatField;
    cdsRAZON: TStringField;
    cdsNETO: TFloatField;
    cdsIVA: TFloatField;
    cdstotalNeto: TAggregateField;
    cdstotalIVA: TAggregateField;
    cdstotal: TAggregateField;
    Label2: TLabel;
    neto: TDBText;
    Label4: TLabel;
    iva: TDBText;
    ppLabel11: TppLabel;
    ppDBText4: TppDBText;
    cdsSUBTOTAL: TFloatField;
    btnCargar: TBitBtn;
    ppNombreObra: TppLabel;
    Label5: TLabel;
    edtFiltro: TEdit;
    ppMostrar: TppLabel;
    ppFiltro: TppLabel;
    btnDesfiltrar: TBitBtn;
    ppParameterList1: TppParameterList;
    btnExcel: TSpeedButton;
    procedure btnVistaPreviaClick(Sender: TObject);
    procedure btnCargarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure grdMaterialTitleClick(Column: TColumn);
    procedure FormCreate(Sender: TObject);
    procedure cdsCalcFields(DataSet: TDataSet);
    procedure chbTodosClick(Sender: TObject);
    procedure rpSueldosObraBeforePrint(Sender: TObject);
    procedure identificadorChange(Sender: TObject);
    procedure identificadorKeyPress(Sender: TObject; var Key: Char);
    procedure grdMaterialCellClick(Column: TColumn);
    procedure btnDesfiltrarClick(Sender: TObject);
    procedure edtFiltroKeyPress(Sender: TObject; var Key: Char);
    procedure identificadorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtFiltroChange(Sender: TObject);
    procedure btnExcelClick(Sender: TObject);
  private
    FnombreObra: string;
    FcolumnaSeleccionada: word;
    procedure SetnombreObra(const Value: string);
    procedure SetcolumnaSeleccionada(const Value: word);
    { Private declarations }
  published
    property nombreObra: string read FnombreObra write SetnombreObra;
    property columnaSeleccionada: word read FcolumnaSeleccionada write SetcolumnaSeleccionada;
  public
    constructor CreateNuevo(AOwner: TComponent; idObra: string;
    fechaInicial, fechaFinal: TDateTime; todos: boolean);
  end;

var
  frmMaterialObra: TfrmMaterialObra;

implementation

uses Recursos, MDatos;

{$R *.dfm}

constructor TfrmMaterialObra.CreateNuevo(AOwner: TComponent; idObra: string;
fechaInicial, fechaFinal: TDateTime; todos: boolean);
begin
  inherited Create(Aowner);
  self.chbTodos.Checked := todos;
  if todos then
  begin
    self.edtFechaIni.Date := IncYear(now, -1);
    self.edtFechaFin.Date := now;
  end
  else
  begin
    self.edtFechaIni.Date := fechaInicial;
    self.edtFechaFin.Date := fechaFinal;
  end;
  if idObra <> '0' then
  begin
    self.identificador.Text := idObra;
    self.btnCargarClick(self);
  end;
  self.ShowModal;
end;

procedure TfrmMaterialObra.btnVistaPreviaClick(Sender: TObject);
begin
  if (self.cds.State in [dsInactive]) then
    self.btnCargarClick(Sender);
  self.rpSueldosObra.DeviceType := dtScreen;
  self.rpSueldosObra.Print;
end;

procedure TfrmMaterialObra.btnCargarClick(Sender: TObject);
begin
  self.cds.Close;
  if self.identificador.Text <> '' then
  begin
    self.cds.Params[0].AsString := self.identificador.Text;
    self.cds.Params[2].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
    if self.chbTodos.Checked then
      self.cds.Params[1].AsString := '01/01/1900'
    else
      self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
    self.cds.Open;
  end
  else
    Application.MessageBox('Debe ingresar el Identificador de Obra', 'Dato Faltante',
    mb_OK+mb_ICONERROR);
end;

procedure TfrmMaterialObra.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  self.cds.Close;
  Action := caFree;
end;

procedure TfrmMaterialObra.grdMaterialTitleClick(Column: TColumn);
begin
  self.cds.IndexFieldNames := Column.FieldName;
end;

procedure TfrmMaterialObra.FormCreate(Sender: TObject);
begin
  acomodo(self);
end;

procedure TfrmMaterialObra.cdsCalcFields(DataSet: TDataSet);
begin
  self.cdssubtotal.AsFloat := self.cdsNETO.AsFloat + self.cdsIVA.AsFloat;
end;

procedure TfrmMaterialObra.chbTodosClick(Sender: TObject);
begin
  self.edtFechaIni.Enabled := not self.chbTodos.Checked;
  self.edtFechaFin.Enabled := not self.chbTodos.Checked;
end;

procedure TfrmMaterialObra.rpSueldosObraBeforePrint(Sender: TObject);
begin
  self.ppNombreObra.Caption := self.nombreObra;
  if self.cds.Filtered then
    self.ppFiltro.Caption := 'Filtrar por: '+self.edtFiltro.Text
  else
    self.ppFiltro.Text := '';
  if self.chbTodos.Checked then
    self.ppMostrar.Caption := 'Mostrar TODOS'
  else
    self.ppMostrar.Caption := 'Mostrar del '+self.edtFechaIni.Text+' al '+self.edtFechaFin.Text;;
end;

procedure TfrmMaterialObra.identificadorChange(Sender: TObject);
var
  ds : TSQLDataSet;
begin
  self.btnCargar.Enabled := self.identificador.Text <> '';
  self.btnVistaPrevia.Enabled := self.btnCargar.Enabled;
  if self.identificador.Text <> '' then
  begin
    ds := TSQLDataSet.Create(self);
    try
      ds.SQLConnection := Datos.Con;
      ds.CommandText := 'SELECT denominacion FROM presupuesto WHERE id_presupuesto = '
      +self.identificador.Text;
      ds.Open;
      if not ds.IsEmpty then
      begin
        self.nombreObra := ds.fields [0].AsString;
        self.Caption := 'MATERIALES POR OBRA - '+self.nombreObra;        
      end;
    finally
      ds.Free;
    end;
  end;
end;

procedure TfrmMaterialObra.identificadorKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not ((StrScan('0123456789',Key) <> nil) or (Key = Char(VK_BACK))) then
    Key := #0;
end;

procedure TfrmMaterialObra.SetnombreObra(const Value: string);
begin
  FnombreObra := Value;
end;

procedure TfrmMaterialObra.grdMaterialCellClick(Column: TColumn);
var
  i : word;
begin
  if (Column.Title.Font.Color <> clRed) and (Column.Field.DataType = ftString)then
  begin
    for i := 0 to self.grdMaterial.Columns.Count-1 do
      self.grdMaterial.Columns [i].Title.Font.Color := Column.Title.Font.Color;
    Column.Title.Font.Color := clRed;
    self.columnaSeleccionada := self.grdMaterial.Col-1;
  end;
end;

procedure TfrmMaterialObra.SetcolumnaSeleccionada(const Value: word);
begin
  FcolumnaSeleccionada := Value;
end;

procedure TfrmMaterialObra.btnDesfiltrarClick(Sender: TObject);
begin
  self.edtFiltro.Text := '';
  self.edtFiltro.SetFocus;
  self.btnCargarClick(Sender);
end;

procedure TfrmMaterialObra.edtFiltroKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = chr(VK_RETURN) then
  begin
    self.cds.Filtered := False;
    if (self.edtFiltro.Text <> '') then
    begin
      self.cds.Filter := self.grdMaterial.Columns [self.columnaSeleccionada].FieldName+' LIKE ''%'+self.edtFiltro.Text+'%''';
      self.cds.Filtered := True;
    end;
  end;
end;

procedure TfrmMaterialObra.identificadorKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
    self.btnCargarClick(Sender);
end;

procedure TfrmMaterialObra.edtFiltroChange(Sender: TObject);
begin
  self.btnDesfiltrar.Enabled := self.edtFiltro.Text <> '';
end;

procedure TfrmMaterialObra.btnExcelClick(Sender: TObject);
begin
  self.btnExcel.Cursor := crHourGlass;
  if self.chbTodos.Checked then
    abrirEnExcel(self, self.cds, nil, nil, 'LISTADO DE MATERIALES - OBRA: '+self.nombreObra,
    'INTERVALO DEFINIDO: Mostrar Todos')
  else
    abrirEnExcel(self, self.cds,  nil, nil, 'LISTADO DE MATERIALES - OBRA: '+self.nombreObra,
    'INTERVALO DEFINIDO: del '+DateTimeToStr(self.edtFechaIni.Date)
    +' al '+DateTimeToStr(self.edtFechaFin.Date));
  self.btnExcel.Cursor := crDefault;
end;

end.
